'use strict'

function generateGraphCode (opt={}){
    let defaultOpt = {
        ...opt
    }
    return async (ctx,next) => {
        if (ctx.request.path !== defaultOpt.url) return await next();
        let {code}= ctx.request.query; 
        ctx.type = 'image/jpeg';
        ctx.body = await ctx.get('/users/generateCaptcha',{token:code},{responseType:"stream"});
    }
}

export default function (app){
     //验证码
    app.use(generateGraphCode({url: '/captcha',}));
    app.use(async (ctx,next) => {
        ctx.checkCaptcha = async (code='')=>{
            let result = await ctx.get('/internal/admins/verifyAuthCode',{code});
            return result.data == true;
        };
        await next();
    });
}
